#ifndef ____inifileloaderbase_____
#define ____inifileloaderbase_____

#include <fstream>
#include <string>
#include <cassert>

#pragma warning(disable:4800)
#pragma warning(disable:4129)


namespace MyCommon
{


	class IniFileLoaderBase
	{
	private:

		//
		//the file the parameters are stored in
		std::ifstream file;

		std::string   CurrentLine;

		void GetParameterValueAsString(std::string& line);
		std::string GetNextParameter();

		//this ignores any comments and finds the next delimited string 
		std::string GetNextToken();
		//this is set to true if the file specified by the user is valid
		bool m_bGoodFile;

	public:
		//
		//helper methods. They convert the next parameter value found into the 
		//relevant type


		double GetNextParameterDouble(){if (m_bGoodFile) return atof(GetNextParameter().c_str());throw std::runtime_error("bad file");}
		float GetNextParameterFloat(){if (m_bGoodFile) return (float)atof(GetNextParameter().c_str());throw std::runtime_error("bad file");}
		int GetNextParameterInt()
		{
			if (m_bGoodFile)
				return atoi(GetNextParameter().c_str());
			throw std::runtime_error("bad file");
		}
		bool GetNextParameterBool(){return (bool)(atoi(GetNextParameter().c_str()));throw std::runtime_error("bad file");}

		double GetNextTokenAsDouble(){if (m_bGoodFile) return atof(GetNextToken().c_str()); throw std::runtime_error("bad file");}
		float GetNextTokenAsFloat(){if (m_bGoodFile) return (float)atof(GetNextToken().c_str()); throw std::runtime_error("bad file");}
		int GetNextTokenAsInt(){if (m_bGoodFile) return atoi(GetNextToken().c_str()); throw std::runtime_error("bad file");}
		std::string GetNextTokenAsString(){if (m_bGoodFile) return GetNextToken(); throw std::runtime_error("bad file");}

		bool eof()const{if (m_bGoodFile) return file.eof(); throw std::runtime_error("bad file");}
		bool FileIsGood()const{return m_bGoodFile;}

		IniFileLoaderBase(const char* filename):CurrentLine(""), m_bGoodFile(true)
		{
			file.open(filename);
			if (file.fail()){m_bGoodFile = false;}
		}
	};

}//namespace MyCommon

#endif